package com.hiof.utility;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
import com.hiof.Configuration;
import com.hiof.R;
import com.hiof.servercommunication.receive.beans.CartesianCoordinate;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import org.apache.http.HttpResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Utility {

    public static final String APP_NAME = "Mora: ";

    public static boolean isOnline(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    public static void vibrate(Context context, int vibrationTime) {
        ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(300);
    }

    /**
     * gets the result string from response
     *
     * @param response
     * @return
     * @throws IOException
     */
    public static String getResultFromHttpResponse(HttpResponse response)
            throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String result = "";
        String line = null;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        return result;
    }

    /**
     * Check the network connection
     *
     * @return
     */
    public static boolean hasNetwork() {
        NetworkInfo info = (NetworkInfo) ((ConnectivityManager) (ConnectivityManager) Configuration.getInstance().getCurrentContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info == null || !info.isConnected()) {
            return false;
        }
        return true;
    }

    /**
     * Create progress bar
     */
    public static ProgressDialog getProgressBar(String message, Context context) {
        if (String.valueOf(message).equals("") || String.valueOf(message).equals("null")) {
            message = Configuration.getInstance().getCurrentContext().getResources().getString(R.string.progress_bar_title);
        }
        if(context == null){
            context = Configuration.getInstance().getCurrentContext();
        }
        final ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setMessage(message);
        progressDialog.setCancelable(true);
        return progressDialog;
    }
    public static String getFormattedDistance(double distance) {
        String returnString = Math.round(distance) + " meters";
        int kmDistance = (int) (distance / 1000);
        int meterDistance = (int) (distance % 1000);
        if (kmDistance >= 1 && kmDistance < 3) {
            if (meterDistance >= 300) {
                returnString = kmDistance + " kilometers and " + meterDistance + " meters";
            } else {
                returnString = kmDistance + " kilometers";
            }
        }else if(kmDistance >= 3){
            returnString = kmDistance + " kilometers";
        }
        return returnString;
    }

    public static String getFormattedTime(double predictedTime) {
        String returnString = Math.round(predictedTime) + " minutes.";
        if(Double.isInfinite(predictedTime) || predictedTime<=0){
            return "";
        }
        int hour = (int) (predictedTime / 60);
        int minutes = (int) (predictedTime % 60);
        if (hour >= 1 && hour < 3) {
            if (minutes != 0) {
                returnString = hour + " hours and " + minutes + " minutes";
            } else {
                returnString = hour + " hours";
            }
        }else if(hour >= 3){
            returnString = hour + " hours";
        }
        return returnString;
    }

    /**
     * Calculate the distance between two geometry points and return the
     * distance measured by km
     *
     * @param p1: geo point 1
     * @param p2: geo point 2
     * @return: distance by meters
     */
    public static double distanceByMeters(GeoPoint p1, GeoPoint p2) {
        if (p1.getAltitude() != 0 && p2.getAltitude() != 0) {
            CartesianCoordinate c1 = p1.toCartersianCartesianCoordinate();
            CartesianCoordinate c2 = p2.toCartersianCartesianCoordinate();
            return CartesianCoordinate.distance(c1, c2);
        } else {
            GeoPoint alternativeP1 = new GeoPoint(p1.getLat(), p1.getLng(), 0);
            GeoPoint alternativeP2 = new GeoPoint(p2.getLat(), p2.getLng(), 0);
            CartesianCoordinate c1 = alternativeP1.toCartersianCartesianCoordinate();
            CartesianCoordinate c2 = alternativeP2.toCartersianCartesianCoordinate();
            return CartesianCoordinate.distance(c1, c2);
        }
    }


    public static void debug(String className, String message) {
        if (message != null && className != null) {
            Log.d(APP_NAME + className, message);
        }
    }

    public static void error(String className, String message) {
        if (message != null && className != null) {
            Log.e(APP_NAME + className, message);
        }
    }

    public static void makeShortToast(String message) {
        Toast.makeText(Configuration.getInstance().getCurrentContext(), message, Toast.LENGTH_SHORT).show();
    }

    public static void makeLongToast(String message) {
        Toast.makeText(Configuration.getInstance().getCurrentContext(), message, Toast.LENGTH_LONG).show();
    }
}
